//
//  ContactDetailView.swift
//  WeChatClient
//
//  Created by AI Assistant on 2025/10/31.
//

import SwiftUI

struct ContactDetailView: View {
    let contact: User
    
    var body: some View {
        ScrollView {
            VStack(spacing: 0) {
                // 头部信息
                ContactHeaderView(contact: contact)
                
                Divider()
                    .padding(.vertical, 20)
                
                // 操作按钮
                ContactActionsView(contact: contact)
                
                Divider()
                    .padding(.vertical, 20)
                
                // 详细信息
                ContactInfoView(contact: contact)
                
                Spacer(minLength: 100)
            }
        }
        .navigationTitle("")
        .navigationBarTitleDisplayMode(.inline)
    }
}

struct ContactHeaderView: View {
    let contact: User
    
    var body: some View {
        VStack(spacing: 16) {
            // 头像
            Image(systemName: contact.avatar)
                .font(.system(size: 80))
                .foregroundColor(.blue)
                .frame(width: 100, height: 100)
                .background(Color.gray.opacity(0.2))
                .clipShape(RoundedRectangle(cornerRadius: 16))
            
            // 姓名和状态
            VStack(spacing: 8) {
                HStack {
                    Text(contact.displayName)
                        .font(.system(size: 22, weight: .medium))
                        .foregroundColor(.primary)
                    
                    if contact.isOnline {
                        Image(systemName: "circle.fill")
                            .font(.system(size: 8))
                            .foregroundColor(.green)
                    }
                }
                
                if !contact.signature.isEmpty {
                    Text(contact.signature)
                        .font(.system(size: 14))
                        .foregroundColor(.secondary)
                        .multilineTextAlignment(.center)
                        .padding(.horizontal)
                }
                
                HStack(spacing: 20) {
                    Text("微信号: \(contact.userName)")
                        .font(.system(size: 12))
                        .foregroundColor(.secondary)
                    
                    Button(action: {
                        // 复制微信号
                        UIPasteboard.general.string = contact.userName
                    }) {
                        Image(systemName: "doc.on.doc")
                            .font(.system(size: 12))
                            .foregroundColor(.secondary)
                    }
                }
            }
        }
        .padding(.horizontal)
        .padding(.top, 20)
    }
}

struct ContactActionsView: View {
    let contact: User
    
    var body: some View {
        HStack(spacing: 40) {
            ContactActionButton(
                icon: "message.fill",
                title: "发消息",
                color: .green
            ) {
                print("发消息给 \\(contact.displayName)")
            }
            
            ContactActionButton(
                icon: "phone.fill",
                title: "语音通话",
                color: .blue
            ) {
                print("语音通话给 \\(contact.displayName)")
            }
            
            ContactActionButton(
                icon: "video.fill",
                title: "视频通话",
                color: .orange
            ) {
                print("视频通话给 \\(contact.displayName)")
            }
        }
        .padding(.horizontal)
    }
}

struct ContactActionButton: View {
    let icon: String
    let title: String
    let color: Color
    let action: () -> Void
    
    var body: some View {
        Button(action: action) {
            VStack(spacing: 8) {
                Image(systemName: icon)
                    .font(.system(size: 20))
                    .foregroundColor(.white)
                    .frame(width: 50, height: 50)
                    .background(color)
                    .clipShape(Circle())
                
                Text(title)
                    .font(.system(size: 12))
                    .foregroundColor(.primary)
            }
        }
    }
}

struct ContactInfoView: View {
    let contact: User
    
    var body: some View {
        VStack(spacing: 0) {
            ContactInfoRow(
                icon: "phone.fill",
                title: "电话号码",
                value: contact.phoneNumber
            )
            
            Divider()
                .padding(.leading, 60)
            
            ContactInfoRow(
                icon: "location.fill",
                title: "地区",
                value: "北京"
            )
            
            Divider()
                .padding(.leading, 60)
            
            ContactInfoRow(
                icon: "person.2.fill",
                title: "共同好友",
                value: "\(Int.random(in: 0...10)) 个"
            )
            
            Divider()
                .padding(.leading, 60)
            
            ContactInfoRow(
                icon: "tag.fill",
                title: "标签",
                value: "同事"
            )
            
            Divider()
                .padding(.leading, 60)
            
            ContactInfoRow(
                icon: "star.fill",
                title: "朋友圈",
                value: "查看朋友圈"
            ) {
                print("查看\\(contact.displayName)的朋友圈")
            }
        }
        .padding(.horizontal)
    }
}

struct ContactInfoRow: View {
    let icon: String
    let title: String
    let value: String
    let action: (() -> Void)?
    
    init(icon: String, title: String, value: String, action: (() -> Void)? = nil) {
        self.icon = icon
        self.title = title
        self.value = value
        self.action = action
    }
    
    var body: some View {
        Button(action: {
            action?()
        }) {
            HStack(spacing: 15) {
                Image(systemName: icon)
                    .font(.system(size: 16))
                    .foregroundColor(.secondary)
                    .frame(width: 20)
                
                Text(title)
                    .font(.system(size: 16))
                    .foregroundColor(.primary)
                
                Spacer()
                
                Text(value)
                    .font(.system(size: 14))
                    .foregroundColor(.secondary)
                
                if action != nil {
                    Image(systemName: "chevron.right")
                        .font(.system(size: 12))
                        .foregroundColor(.secondary)
                }
            }
            .padding(.vertical, 12)
        }
        .buttonStyle(PlainButtonStyle())
        .disabled(action == nil)
    }
}
